package com.link.base.base.mvg.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.mvg.model.Mvg;
import com.link.base.base.mvg.service.MvgService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.util.UserUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments mvg请求处理
 */
@Controller
@Scope("prototype")
@RequestMapping("/link/mvg")
public class MvgController {

    @Resource
    private MvgService mvgService;

    @RequestMapping(value = "/queryLeftListPage")
    @ResponseBody
    public Map<String, Object> queryLeftListPage(@JsonParam QueryParams qps, @JsonParam Mvg entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            qps.setUserOrgId(user.getOrgId());
            qps.setUserPositionId(user.getPostnId());
            mvgService.setMvgMapperName(qps.getMvgMapperName());
            BasicModel t = (BasicModel) BasicModel.transformClass(entity, qps);
            List<HashMap<String, String>> list = mvgService.queryLeftListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", BasicController.getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Mvg>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/queryRightListPage")
    @ResponseBody
    public Map<String, Object> queryRightListPage(@JsonParam QueryParams qps, @JsonParam Mvg entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            qps.setUserOrgId(user.getOrgId());
            qps.setUserPositionId(user.getPostnId());
            mvgService.setMvgMapperName(qps.getMvgMapperName());
            BasicModel t = (BasicModel) BasicModel.transformClass(entity, qps);
            List<HashMap<String, String>> list = mvgService.queryRightListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", BasicController.getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Mvg>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/batchInsert")
    @ResponseBody
    public Map<String, Object> batchInsert(@RequestBody List<Mvg> list, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //默认子ID
            Long defMvgStId = null;
            if (list != null && list.size() > 0) {
                mvgService.setMvgMapperName(list.get(0).getMvgMapperName());
                defMvgStId = mvgService.batchInsert(list);
            }
            result.put("defMvgStId", defMvgStId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/insert")
    @ResponseBody
    public Map<String, Object> create(@JsonParam Mvg entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            mvgService.setMvgMapperName(entity.getMvgMapperName());
            mvgService.insert(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Map<String, Object> batchDelete(@RequestBody List<Mvg> list, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //默认子ID
            Long defMvgStId = null;
            if (list != null && list.size() > 0) {
                mvgService.setMvgMapperName(list.get(0).getMvgMapperName());
                defMvgStId = mvgService.batchDeleteById(list);
            }
            result.put("defMvgStId", defMvgStId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/deleteAll")
    @ResponseBody
    public Map<String, Object> deleteAll(@JsonParam Mvg entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            mvgService.setMvgMapperName(entity.getMvgMapperName());
            mvgService.deleteAllById(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(@JsonParam Mvg entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("delete")
                .withMessage("删除一条列表信息...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            mvgService.setMvgMapperName(entity.getMvgMapperName());
            mvgService.deleteById(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 一次性修改mvg信息，包括删除、新建中间表数据，更新主表主要字段
     *
     * @author yrf
     * @param list
     * @return
     */
    @RequestMapping(value = "/mvgModify")
    @ResponseBody
    public Map<String, Object> mvgModify(@RequestBody List<Mvg> list) {
        LCLogger.info().withMessageKey("mvgModify")
                .withMessage("MVG信息修改...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (list.size() > 0) {
                mvgService.setMvgMapperName(list.get(0).getMvgMapperName());
                mvgService.mvgModify(list);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
